using Godot;
using Vector2 = Godot.Vector2;

public enum SlotType
{
    //存储，关闭及掉落，只输入，制作
    STORE, CLOUSEDORP, INPUTONLY, Craft
}
public class Slot : Control
{
    public ItemStack itemStack;
    public SlotType Type = SlotType.STORE;
    public int index;
    public bool HighLight = true;
    public Sprite hl;
    public TextureRect sprite;
    Label label;
    TextureProgress textureProgress;
    public delegate void OnClick(Slot slot);
    public OnClick HoldIn;
    public OnClick HoldOver;
    public OnClick OnClickEvent;
    public OnClick OnRightClickEvent;
    public OnClick OnShiftClickEvent;

    public float tick = 0;
    public override void _Ready()
    {
        sprite = GetNode<TextureRect>("TextureRect");
        hl = GetNode<Sprite>("HighLight");
        label = GetNode<Label>("Amount");
        textureProgress = GetNode<TextureProgress>("TextureProgress");
    }
    public void SetClick(OnClick oc) { this.OnClickEvent = oc; }
    public void SetRight(OnClick oc) { this.OnRightClickEvent = oc; }
    public void SetShiftClick(OnClick oc) { this.OnShiftClickEvent = oc; }
    public override void _Process(float delta)
    {
        if (!Visible)
        {
            return;
        }
        const double s = 2 * 0.5;
        Vector2 mv = GetGlobalMousePosition();
        Vector2 sv = GetGlobalPosition();
        if (
            mv.x > sv.x && mv.x < sv.x + 16 * s &&
            mv.y > sv.y && mv.y < sv.y + 16 * s
        )
        {
            if (hl.Visible == false)
                if (HoldIn != null) HoldIn(this);
            if (HighLight) hl.Visible = true;

            if (tick <= 0)
            {
                if (Input.IsActionPressed("click"))
                {

                    if (Input.IsActionPressed("shift"))
                    {
                        if (OnShiftClickEvent != null)
                        {
                            OnShiftClickEvent(this);
                            tick = 0.1f;
                        }
                    }
                    else
                    {
                        //GD.Print($"Click At{index}");
                        if (OnClickEvent != null)
                        {
                            //GD.Print("true");
                            OnClickEvent(this);
                            tick = 0.2f;
                        }
                        else
                        {
                            //GD.Print("false");
                        }
                    }
                }
                else
                if (Input.IsActionPressed("use"))
                {
                    if (OnRightClickEvent != null)
                    {
                        OnRightClickEvent(this);
                        tick = 0.2f;
                    }
                }
            }
            else
            {
                tick -= delta;
            }


        }
        else if (hl.Visible == true)
        {
            if (HoldOver != null) HoldOver(this);
            if (HighLight) hl.Visible = false;
        }
    }
    public void SetItemStack(ItemStack item)
    {
        this.itemStack = item;
        if (item == null)
        {
            sprite.Texture = null;
            label.Text = "";
            textureProgress.Visible = false;
        }
        else
        {
            if (item.getMaterial() == null)
            {
                throw new WorldException($"err !{item.toJson()}:");
            }
            sprite.Texture = item.getMaterial().texture;
            if (item.Amount > 1) label.Text = item.Amount.ToString();
            else label.Text = "";

            if (item.itemData != null && item.itemData is ToolData data)
            {
                ToolData td = data;
                textureProgress.Visible = true;
                textureProgress.Value = ((float)td.Durable / (float)td.Max) * 100.0;
                if (textureProgress.Value == 100)
                    textureProgress.Visible = false;
                else
                    textureProgress.Visible = true;
            }
            else
            {
                textureProgress.Visible = false;
            }
        }

    }
    public void MoveToMouse()
    {
        HighLight = false;
        Vector2 mouse = GetViewport().GetMousePosition() * 0.5f;
        SetPosition(mouse);
    }
}
